package com.example.design.factory.impl;

import com.alibaba.fastjson.JSON;
import com.example.design.factory.ICommodity;
import com.example.design.goods.DeliverReq;
import com.example.design.goods.GoodsService;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @version 1.0.0
 * @className: GoodsCommodityService
 * @description: 实现奖品发放接口  -  实物商品
 * @author: rosszhang
 * @date: 2022/5/31 15:27
 */
@Slf4j
public class GoodsCommodityService implements ICommodity {

    private GoodsService goodsService = new GoodsService();

    /**
     * 所有的奖品无论是实物、虚拟还是第三方，都需要通过我们的程序实现此接口进行处理，以保证最终入参出参的统一性。
     *
     * @param uId         : 用户ID
     * @param commodityId : 奖品ID
     * @param bizId       : 业务ID
     * @param extMap      : 扩展字段 - 用于处理发放实物商品时的收获地址。
     * @return void
     * @author rosszhang
     * @date 2022/5/31 15:21
     */
    @Override
    public void sendCommodity(String uId, String commodityId, String bizId, Map<String, String> extMap) {
        DeliverReq deliverReq = new DeliverReq();
        deliverReq.setUserName(queryUserName(uId));
        deliverReq.setUserPhone(queryUserPhoneNumber(uId));
        deliverReq.setSku(commodityId);
        deliverReq.setOrderId(bizId);
        deliverReq.setConsigneeUserName(extMap.get("consigneeUserName"));
        deliverReq.setConsigneeUserPhone(extMap.get("consigneeUserPhone"));
        deliverReq.setConsigneeUserAddress(extMap.get("consigneeUserAddress"));

        Boolean isSuccess = goodsService.deliverGoods(deliverReq);

        log.info("请求参数[优惠券] => uId：{} commodityId：{} bizId：{} extMap：{}", uId, commodityId, bizId, JSON.toJSON(extMap));
        log.info("测试结果[优惠券]：{}", isSuccess);

        if (!isSuccess) {
            throw new RuntimeException("实物商品发放失败");
        }
    }

    private String queryUserName(String uId) {
        return "花花";
    }

    private String queryUserPhoneNumber(String uId) {
        return "15200101232";
    }
}
